package com.gexiaobao.pigeon.ui.fragment.mine.organization.detail

import android.os.Bundle
import android.text.TextUtils
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.loadListData
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.ext.showLoading
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.FragmentOrgMatchBinding
import com.gexiaobao.pigeon.ui.activity.ActivityGameResult
import com.gexiaobao.pigeon.ui.adapter.OrgMatchAdapter
import com.gexiaobao.pigeon.viewmodel.OrgDetailViewModel
import com.kingja.loadsir.core.LoadService
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction
import me.hgj.jetpackmvvm.util.startActivity

/**
 * @Author      : hxw
 * @Date        : 2023/5/31 16:10
 * @Describe    : 赛事
 */
class FragmentOrgMatch : BaseFragment<OrgDetailViewModel, FragmentOrgMatchBinding>() {

    private val mAdapter: OrgMatchAdapter by lazy { OrgMatchAdapter() }
    private lateinit var loadsir: LoadService<Any>

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.layoutList.swipeRecyclerView.init(LinearLayoutManager(context), mAdapter)
            .addItemDecoration(SpaceItemDecoration(0, Util.dp2px(context, 5f)))

        mDatabind.layoutList.swipeRefreshLayout.init {
            loadsir.showLoading()
            mViewModel.getOrgRaceListApp(true, mOrgId)
        }

        //状态页配置
        loadsir = loadServiceInit(mDatabind.layoutList.swipeRefreshLayout) {
            //点击重试时触发的操作
            loadsir.showLoading()
            mViewModel.getOrgRaceListApp(true, mOrgId)
        }

        mAdapter.addChildClickViewIds(R.id.go_race_pass)
        mAdapter.setOnItemChildClickListener { _, _, position ->
            if (mAdapter.data[position].racingPassId == 0 || TextUtils.isEmpty(mAdapter.data[position].name)) {
                RxToast.showToast(getString(R.string.There_are_no_ongoing_levels))
                return@setOnItemChildClickListener
            }
            startActivity<ActivityGameResult>(
                "title" to mAdapter.data[position].name,
                "passId" to mAdapter.data[position].racingPassId,
                "type" to "race"
            )
            activity?.overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
        }

        mAdapter.run {
            setOnItemClickListener { _, _, position ->
                nav().navigateAction(R.id.action_org_detail_to_match, Bundle().apply {
                    putString("title", mAdapter.data[position].name)
                    putInt("id", mAdapter.data[position].id)
                    putInt("racingPassId", mAdapter.data[position].racingPassId)
                    putInt("followStatus", mFollowStatus)
                })
            }
        }
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        loadsir.showLoading()
        mViewModel.getOrgRaceListApp(true, mOrgId)
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.orgMatchResult.observe(viewLifecycleOwner) {
            if (it.isSuccess) {
                loadListData(it, mAdapter, loadsir, mDatabind.layoutList.swipeRecyclerView, mDatabind.layoutList.swipeRefreshLayout)
            } else {
                RxToast.showToast(it.errMessage)
            }
        }
    }

    companion object {

        private var mOrgId = ""
        private var mFollowStatus = 0
        fun newInstance(orgId: String, followStatus: Int): FragmentOrgMatch {
            mOrgId = orgId
            mFollowStatus = followStatus
            return FragmentOrgMatch()
        }
    }
}